package com.graduate.front.controller;

import com.graduate.common.entity.House;
import com.graduate.common.entity.Reserve;
import com.graduate.common.entity.Result;
import com.graduate.common.entity.Users;
import com.graduate.common.exception.UserException;
import com.graduate.common.vo.ReserveVo;
import org.apache.catalina.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author bwzfy
 * @create 2021/5/11
 **/
@Controller
@RequestMapping("/reserve")
public class ReserveController extends BaseController{

    @ResponseBody
    @GetMapping("/login")
    public String login(HttpSession session) {
        Users users = new Users();
        users.setId(1);
        users.setEmail("123@qq.com");
        users.setIdCard("122312312312321");
        users.setPhoneNum("10086");
        users.setUsername("admin");
        users.setRole(0);
        session.setAttribute("user", users);
        return "success";
    }

    /**
     * 普通方式预定/vip方式预定
     * 把当前的用户预订信息存入数据库中
     * @return
     */
    @ResponseBody
    @PostMapping("/house")
    public Result<String> reserveHouse(@RequestBody Reserve reserve, HttpSession session) {
        // 从session中获取用户信息
        Users user = (Users) session.getAttribute("user");
        reserve.setUserId(user.getId());
        Result<String> result = new Result<>();
        // 普通预定房间
        try {
            reserveService.reserveHouse(reserve);
            result.setCode(200);
            return result;
        } catch (UserException e) {
            e.printStackTrace();
            // 把异常信息封装入返回信息中
            result.setCode(500);
            result.setData(e.getMessage());
        }
        return result;
    }

    @GetMapping("/house/{hid}")
    public String toReserveHouse(@PathVariable("hid") String hid, Map<String, Object> map) throws UserException {
        // 查询出这个hid对应的房间信息并且存到request域中，返回给前端
        House house = houseService.getById(hid);
        if (house == null) {
            throw new UserException("无法查询出房间信息");
        }
        map.put("house", house);
        return "client/BookInfo";
    }

    /**
     * 以组团的方式去预定房间
     * @param reserve
     * @return
     */
    @PostMapping("/group")
    public Result<String> reserveHouseGroup(@RequestBody Reserve reserve) {
        return null;
    }

    /**
     * 根据传入的用户名称和电话号码查询当前用户是否已注册
     * @param reserve
     * @return
     */
    @PostMapping("/search")
    public Result<ReserveVo> searchReserve(@RequestBody ReserveVo reserve) {
        ReserveVo reserveVo = reserveService.selectReserveByPhoneOrName(reserve.getTelephone(), reserve.getRealName());
        return null;
    }
}
